package com.zdsoft.blp.zongbuju.wode.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.zongbuju.wode.entity.LiuYan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/2.
 */
public class LiuYanAdapter extends BaseAdapter {
    private List<LiuYan> list;
    private Context context;
    private LayoutInflater inflater;
    private ViewHodler hodler;
    private AnimationDrawable animationDrawable;
    private List<ImageView> imageViews=new ArrayList<>();
    public void setList(List<LiuYan> list) {
        this.list = list;
    }

    public LiuYanAdapter(Context context, List<LiuYan> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        hodler = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.wodeliuyan_wode_item, null);

            hodler = new ViewHodler();

            hodler.iv_touxiang = (ImageView) convertView.findViewById(R.id.iv_touxiang);
            hodler.iv_chat = (ImageView) convertView.findViewById(R.id.iv_chat);
            imageViews.add(hodler.iv_chat);
            hodler.iv_picture = (ImageView) convertView.findViewById(R.id.iv_picture);

            hodler.tv_mingzi = (TextView) convertView.findViewById(R.id.tv_mingzi);
            hodler.tv_shijian = (TextView) convertView.findViewById(R.id.tv_shijian);
            hodler.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            hodler.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            hodler.ll_yinpin = (LinearLayout) convertView.findViewById(R.id.ll_yinpin);
            hodler.gv = (GridView) convertView.findViewById(R.id.gv);
            hodler.rl_chat = (RelativeLayout) convertView.findViewById(R.id.rl_chat);

            convertView.setTag(hodler);
        } else {
            hodler = (ViewHodler) convertView.getTag();
        }

        final LiuYan liuyan = list.get(position);
        MyApp.setImage(liuyan.getHeadImg(), hodler.iv_touxiang, true);
        hodler.tv_mingzi.setText(liuyan.getUserName());
        hodler.tv_shijian.setText(liuyan.getCreateTime());
        hodler.tv_content.setText(liuyan.getContent());
        hodler.tv_time.setText( liuyan.getVflength()+"″");
        final String path=liuyan.getAmrPath();
        if ("".equals(path)) {
            hodler.ll_yinpin.setVisibility(View.GONE);
        } else {
            hodler.ll_yinpin.setVisibility(View.VISIBLE);
        }

        hodler.rl_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,position+"", Toast.LENGTH_SHORT).show();
                playNet(imageViews.get(position),path,R.drawable.anima_chat,R.drawable.chatfrom_voice_playing);
            }
        });

//                new Thread() {
//                    @Override
//                    public void run() {
//                        super.run();
//                        try {
//                            MediaPlayer player=new MediaPlayer();
//                            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                                @Override
//                                public void onPrepared(MediaPlayer mp) {
//                                    startAnima(hodler.iv_chat);
//                                }
//                            });
//                            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//                                @Override
//                                public void onCompletion(MediaPlayer mp) {
//                                    stopAnima();
//                                }
//                            });
//                            player.reset();
//                            player.setDataSource(liuyan.getAmrPath());
//                            player.prepare();
//                            player.start();
//
//                            Message msg = handler.obtainMessage();
//                            msg.what = 1;
//                            handler.sendMessage(msg);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }.start();





        List<String> picPathlList = liuyan.getPicPathSet();
        if (picPathlList.size() == 0) {
            hodler.gv.setVisibility(View.GONE);
        } else {
            hodler.gv.setVisibility(View.VISIBLE);
            hodler.gv.setAdapter(new LiuYanItemGridAdapter(picPathlList, context));
        }

        return convertView;
    }

    class ViewHodler {
        ImageView iv_touxiang, iv_chat, iv_picture;
        TextView tv_mingzi, tv_shijian, tv_content, tv_time;
        LinearLayout ll_yinpin;
        RelativeLayout rl_chat;
        GridView gv;
    }

//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == 1) {
//                if (animationDrawable != null && animationDrawable.isRunning()) { //如果正在运行,就停止
//                    hodler.iv_chat.setBackgroundResource(R.drawable.chatfrom_voice_playing_f3);
//
//                }
//            }
//        }
//    };

    private void startAnima(ImageView iv){
        if(animationDrawable==null){
            hodler.iv_chat.setImageResource(R.drawable.anima_chat);
            animationDrawable = (AnimationDrawable) iv.getDrawable();
        }
        animationDrawable.start();
    }


    private void stopAnima() {
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
        }

    }




    public static void playNet(final ImageView imgvoice, final String videoUrl,
                               final int animationDrawable, final int src) {
        try {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        imgvoice.setImageResource(animationDrawable);
                        final AnimationDrawable anima = (AnimationDrawable) imgvoice.getDrawable();
                        if (anima.isRunning()) {
                            anima.stop();
                        }
                        MediaPlayer mediaPlayer = new MediaPlayer();
                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                            @Override
                            public void onPrepared(MediaPlayer arg0) {
                                anima.start();
                            }
                        });
                        //结束后关闭声音播放动画
                        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                            @Override
                            public void onCompletion(MediaPlayer arg0) {
                                anima.stop();
                                imgvoice.setImageResource(src);
                            }
                        });
                        /* 重置MediaPlayer */
                        mediaPlayer.reset();
                        /* 设置要播放的文件的路径 */
                        mediaPlayer.setDataSource(videoUrl);

                        Log.e("videoUrl", videoUrl);
                        /* 准备播放 */
                        mediaPlayer.prepareAsync();
                        /* 开始播放 */
                        mediaPlayer.start();
                    } catch (Exception e) {
                        Log.e("mediaPlayer", "error", e);
                    }
                }
            }, 0);
        } catch (Exception e) {
            Log.e("mediaPlayer", "error", e);
        }
    }

}
